
package com.hdoit.ares.web.acc.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.acc.model.CustomerDemo;
import com.hdoit.ares.logical.acc.service.IAwardSettingService;
import com.hdoit.ares.logical.acc.service.ICustomerDemoService;
import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.cust.service.ICustomerService;
import com.hdoit.ares.logical.viewmodel.acc.CustomerDemoSearch;
import com.hdoit.ares.selfcommon.enums.acc.InsuranceType;
import com.hdoit.ares.selfcommon.utils.DistributeWealthDemoUtil;
import com.hdoit.ares.selfcommon.utils.ExpressionUtil;
import com.tennetcn.common.aop.CommonApplicationContextUtil;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.YesOrNo;
import com.tennetcn.common.logical.setting.model.ParamOption;
import com.tennetcn.common.logical.setting.service.IParamOptionService;
import com.tennetcn.common.logical.viewmodel.setting.ParamOptionSearch;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

/**
 * @author      yujinhui
 * @email       
 * @createtime  2017年11月29日 12:04:37
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/acc/customerDemo/")
public class CustomerDemoController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/admin/acc/customerDemo/";
    }

    private ICustomerDemoService customerDemoService;
    public ICustomerDemoService getCustomerDemoService() {
        return customerDemoService;
    }
    @Resource
    public void setCustomerDemoService(ICustomerDemoService customerDemoService) {
        this.customerDemoService = customerDemoService;
    }
    
    private IAwardSettingService awardSettingService;
    public IAwardSettingService getAwardSettingService() {
		return awardSettingService;
	}
    @Resource
	public void setAwardSettingService(IAwardSettingService awardSettingService) {
		this.awardSettingService = awardSettingService;
	}

    private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="customerDemoDetailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";
    private final String toTestViewName = "toTest";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,CustomerDemoSearch search,boolean ajaxPost){
    	/*test();*/
        pagerModel.setTotalAmount(customerDemoService.queryCount(search));
        List<CustomerDemo> customerDemos= customerDemoService.queryList(search,pagerModel);

        model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("customerDemos", customerDemos);
        model.addAttribute("customerDemoSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }
    
    public void test(){
    	ICustomerService customerService = CommonApplicationContextUtil.getCurrentContext().getBean(ICustomerService.class);
		List<Customer> customers = customerService.queryList();
		
		IParamOptionService paramOptionService =  CommonApplicationContextUtil.getCurrentContext().getBean(IParamOptionService.class);
		ParamOptionSearch paramOptionSearch = new ParamOptionSearch();
		
		
		for(Customer customer:customers){
			String str = customer.getRegion();
			System.err.println(str);
			
			if(!StringUtils.isEmpty(str)){
				String[] strs = str.split(" ");
				paramOptionSearch.setParamValue(strs[0]);
				paramOptionSearch.setParentId(null);
				ParamOption paramOption = paramOptionService.queryModel(paramOptionSearch);
				
				if(paramOption != null){
					customer.setProvinceId(paramOption.getId());
				
				
					paramOptionSearch.setParamValue(strs[1]);
					paramOptionSearch.setParentId(paramOption.getId());
					ParamOption paramOption1 = paramOptionService.queryModel(paramOptionSearch);
					if(paramOption1 != null)
						customer.setCityId(paramOption1.getId());
				
						if(strs.length == 3){
							paramOptionSearch.setParamValue(strs[2]);
							paramOptionSearch.setParentId(paramOption1.getId());
							ParamOption paramOption2 = paramOptionService.queryModel(paramOptionSearch);
							
							if(paramOption2 != null){
								customer.setDistrictId(paramOption2.getId());
							}
						
						}
						customerService.updateModel(customer);
				   }
				}
				
			}
    }
    
    @RequestMapping(value = toTestViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,String key){
        double promote,firstShare,secondShare,server,hatch;
        double finalPercent = 0.5;
        String str_promote = awardSettingService.queryModelByHC("promote", InsuranceType.NotCar).getExpression();
		promote = ExpressionUtil.stringToDouble(str_promote,finalPercent);
		
		String str_firstShare = awardSettingService.queryModelByHC("firstShare",  InsuranceType.NotCar).getExpression();
		firstShare = ExpressionUtil.stringToDouble(str_firstShare,finalPercent);
		
		String str_secondShare = awardSettingService.queryModelByHC("secondShare", InsuranceType.NotCar).getExpression();
		secondShare = ExpressionUtil.stringToDouble(str_secondShare,finalPercent);
		
		String str_server = awardSettingService.queryModelByHC("server", InsuranceType.NotCar).getExpression();
		server = ExpressionUtil.stringToDouble(str_server,finalPercent);
		
		String str_hatch = awardSettingService.queryModelByHC("hatch", InsuranceType.NotCar).getExpression();
		hatch = ExpressionUtil.stringToDouble(str_hatch,finalPercent);
        
        //测试
        List<CustomerDemo> customerDemos = customerDemoService.queryListByBuyer(key);
        customerDemos = DistributeWealthDemoUtil.toDistribute(promote, firstShare, secondShare, server, hatch, customerDemos);
        
        model.addAttribute("cusDemos", customerDemos);
        return view(toTestViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String customerDemoDetailed(Model model,String key,String referee,String cusUserIdentity){
        CustomerDemo customerDemo=new CustomerDemo();
        if(!StringUtils.isEmpty(key)){
            customerDemo=customerDemoService.queryModel(key);
        }
        
        if(!StringUtils.isEmpty(referee)){
        	model.addAttribute("referee", referee);
        }
        
        if(!StringUtils.isEmpty(cusUserIdentity)){
        	model.addAttribute("cusUserIdentity", cusUserIdentity);
        }
       
        model.addAttribute("customerDemo", customerDemo);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,CustomerDemo customerDemo){
    	if(StringUtils.isEmpty(customerDemo.getId())){
    		int index = customerDemoService.queryMaxUserIdentity();
    		customerDemo.setUserIdentity(index + 1);
            customerDemo.setId(CommonUtils.getKeyId());
            customerDemo.setModelStatus(ModelStatus.add);
            customerDemo.setRegistDate(DateUtils.getCurrent());
            customerDemo.setDeleteMark(YesOrNo.no);
            if(StringUtils.isEmpty(customerDemo.getRefereeId())){
            	customerDemo.setRefereeIdentity(0);
            }
           
            
        }else{
            customerDemo.setModelStatus(ModelStatus.update);
        }

        ActionResult actionResult;
        if(customerDemoService.applyChange(customerDemo)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("customerDemo", customerDemo);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(customerDemoService.deleteModel(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
}
